package com.pachong.android.frameworkbase.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

/**
 * Glide 的辅助类
 *
 * @Author: PWW
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/12/26 18:20
 * @Version: 1.0.0
 */
public class GlideHelper {


    /**
     * 将图片设置成圆形
     *
     * @param context
     * @param imageView
     * @return
     */
    public static BitmapImageViewTarget getBitmapCircularTarget(final Context context, final ImageView imageView, final float radius) {
        return new BitmapImageViewTarget(imageView) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCornerRadius(radius);
                imageView.setImageDrawable(circularBitmapDrawable);
            }
        };
    }


    /**
     * 加载图片， 并且转成圆角矩形
     * @param context
     * @param targetImageView
     * @param imgUriStr
     */
    public static final void loadImageWithRoundRect(Context context, ImageView targetImageView, String imgUriStr, int xRadiusDp, int yRadiusDp) {
        Glide.with(context).load(imgUriStr)
                .transform(new GlideHelper.RoundRectTransformation(context, xRadiusDp, yRadiusDp)).into(targetImageView);
    }

    /**
     * 加载图片， 并且转成圆角矩形
     * @param context
     * @param targetImageView
     * @param imgUriStr
     */
    public static final void loadImageWithRoundRect(Context context, ImageView targetImageView, String imgUriStr, int radiusDp) {
        Glide.with(context).load(imgUriStr)
                .transform(new GlideHelper.RoundRectTransformation(context, radiusDp)).into(targetImageView);
    }


    /**
     * 加载图片， 并且转成圆形
     * @param context
     * @param targetImageView
     * @param imgUriStr
     */
    public static final void loadImageWithRound(Context context, ImageView targetImageView, String imgUriStr ) {
        Glide.with(context).load(imgUriStr)
                .transform(new GlideHelper.RoundTransformation(context)).into(targetImageView);
    }



    /**
     * 圆形
     */
    public static class RoundTransformation extends BitmapTransformation {
        public RoundTransformation(Context context) {
            super(context);
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }

        private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;

            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap squared = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (squared == null) {
                squared = Bitmap.createBitmap(source, x, y, size, size);
            }


            Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            return result;
        }

        @Override
        public String getId() {
            return getClass().getName();
        }
    }


    /**
     * 圆角矩形
     *
     * 注意：如果ImageView设置成CenterCrop， 这个设置的圆角可能会被切掉，所以有可能就没有圆角的效果， 建议用CardView套一个ImageView来处理。
     */
    public static class RoundRectTransformation extends BitmapTransformation {

        private float xRadius = 0f;
        private float yRadius = 0f;


        public RoundRectTransformation(Context context, int radiusDp) {
            this(context, radiusDp, radiusDp);
        }


        public RoundRectTransformation(Context context, int xRadiusDp, int yRadiusDp) {
            super(context);
            this.xRadius = Resources.getSystem().getDisplayMetrics().density * xRadiusDp;
            this.yRadius = Resources.getSystem().getDisplayMetrics().density * yRadiusDp;
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return roundCrop(pool, toTransform);
        }

        private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;

            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }

            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, xRadius, yRadius, paint);
            return result;
        }

        @Override
        public String getId() {
            return getClass().getName() + Math.round(xRadius);
        }
    }


}
